﻿
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.Common;
using Apps.BLL.Core;
using System.Transactions;
using Apps.IDAL;
using Apps.IBLL;
using Apps.Models.Sys;
using Apps.Locale;

namespace Apps.BLL.Sys
{
    public partial class SysUserConfigBLL
    {

        public SysUserConfigModel GetById(string id, string userId)
        {

            SysUserConfig entity = m_Rep.GetById(id, userId);
            if (entity == null)
            {
                return null;
            }
            SysUserConfigModel model = new SysUserConfigModel();
            model.Id = entity.Id;
            model.Name = entity.Name;
            model.Value = entity.Value;
            model.Type = entity.Type;
            model.State = entity.State;
            model.UserId = entity.UserId;
            return model;

        }

        public override bool Edit(ref ValidationErrors errors, SysUserConfigModel model)
        {
            try
            {
                SysUserConfig entity = m_Rep.GetById(model.Id,model.UserId);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.Id = model.Id;
                entity.Name = model.Name;
                entity.Value = model.Value;
                entity.Type = model.Type;
                entity.State = model.State;
                entity.UserId = model.UserId;

                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }


        public override bool Create(ref ValidationErrors errors, SysUserConfigModel model)
        {
            try
            {
                SysUserConfig entity = m_Rep.GetById(model.Id,model.UserId);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new SysUserConfig();
                entity.Id = model.Id;
                entity.Name = model.Name;
                entity.Value = model.Value;
                entity.Type = model.Type;
                entity.State = model.State;
                entity.UserId = model.UserId;

                
                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }


    }
}
